<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 例子控制器
 */
class Controller_Pc_Provider extends Stourweb_Controller
{
    public $_id = '';
    public function before()
    {
        parent::before();
        $this->_id = Cookie::get('st_supplier_id');
		$sql = "SHOW TABLES LIKE 'sline_supplier_provider_config'";
        $result = DB::query(Database::SELECT,$sql)->execute()->valid();
        if(!$result){
            $this->request->redirect('pc');
        }
    }

    public function action_config()
    {
        $config = DB::select()->from('supplier_provider_config')->where("supplier_id={$this->_id}")->execute()->current();
        if($config){
            $ota_list = DB::select()->from('supplier_provider_ota')->execute()->as_array();
            $this->assign('ota_list',$ota_list);
        }
        $this->assign('config',$config);
        $this->display('provider');
    }

    public function action_ajax_sava_partner()
    {
        $partner_id = $_POST['partner_id'];
        $data = array(
            'supplier_id' => $this->_id,
            'partner_id'  => $partner_id
        );
        $is_exist = DB::select()->from('supplier_provider_config')->where("supplier_id={$this->_id}")->execute()->current();
        if($is_exist){
            DB::update('supplier_provider_config')->set($data)->where("id={$is_exist['id']}")->execute();
        }else{
            DB::insert('supplier_provider_config', array_keys($data))->values(array_values($data))->execute();
        }
        echo json_encode(array('status'=>true));
    }
}